﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Script.Serialization;
using DeVetma.admin.models.entities;
using DeVetma.admin.models.managers;
using System.Web.Services;

namespace DeVetma.admin
{
    public partial class manageFemaleClothes : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        [WebMethod]
        public static string deleteClothes(string cID)
        {
            ClothesManager cm = new ClothesManager();
            JavaScriptSerializer jsz = new JavaScriptSerializer();

            if (string.IsNullOrWhiteSpace(cID))
                return jsz.Serialize(new
                {
                    succeeded = false,
                    message = "Invalid cID - "
                        + "empty or null."
                });

            bool succeeded = cm.delete(new Clothes { cID = cID });

            return jsz.Serialize(new
            {
                succeeded = succeeded,
                message = (succeeded ? "The "
                    + "apparel with id: " + cID + " has been successfully deleted." :
                    "Unable to delete the apparel with id: " + cID + " - either " +
                    "it is not found or certain condition prevented it from being deleted.")
            });
        }

        [WebMethod]
        public static string getFemaleClothes()
        {
            ClothesManager cm = new ClothesManager();
            JavaScriptSerializer jsz = new JavaScriptSerializer();

            List<Clothes> results = cm.getFemaleClothes();

            return jsz.Serialize(new
            {
                succeeded = results != null,
                message = results == null ? "Database error. Please contact site admin." : "",
                results = results
            });
        }

        [WebMethod]
        public static string setSellingPrice(string cID, decimal price)
        {
            ClothesManager cm = new ClothesManager();
            JavaScriptSerializer jsz = new JavaScriptSerializer();

            if (string.IsNullOrWhiteSpace(cID))
                return jsz.Serialize(new
                {
                    succeeded = false,
                    message = "Invalid cID - "
                        + "empty or null."
                });

            Clothes clothe = cm.get(cID);
            if (clothe == null)
                return jsz.Serialize(new
                {
                    succeeded = false,
                    message = "Clothe with cID: " + cID + " is not found."
                });

            clothe.cSelling = price;
            bool updated = cm.update(clothe);

            return jsz.Serialize(new
            {
                succeeded = updated,
                message = !updated ? "Database error. Please contact site admin." :
                    "The selling price for the apparel cID " + cID + " has been updated.",
                results = price
            });
        }

        [WebMethod]
        public static string setQuantity(string cID, int qty)
        {
            ClothesManager cm = new ClothesManager();
            JavaScriptSerializer jsz = new JavaScriptSerializer();

            if (string.IsNullOrWhiteSpace(cID))
                return jsz.Serialize(new
                {
                    succeeded = false,
                    message = "Invalid cID - "
                        + "empty or null."
                });

            Clothes clothe = cm.get(cID);
            if (clothe == null)
                return jsz.Serialize(new
                {
                    succeeded = false,
                    message = "Clothe with cID: " + cID + " is not found."
                });

            clothe.cQuanOnHand = qty;
            bool updated = cm.update(clothe);

            return jsz.Serialize(new
            {
                succeeded = updated,
                message = !updated ? "Database error. Please contact site admin." :
                    "The quantity on hand for the apparel cID " + cID + " has been updated.",
                results = qty
            });
        }
    }
}